package com.icarbonx.dap.notification.msgservice.rabbitmq

import com.rabbitmq.client.Connection

import scala.collection.mutable.ArrayBuffer

/**
 * Created by Lenovo on 2018/8/13.
 */
case class MQObject(val host: String,
                    val port: Int,
                    val username: String,
                    val password: String,
                    val virtualhost: String) extends Serializable {
  val serialVersionUID = 5348418242799806967L

  def connect(): Connection = {
    ConnUtil.connect(this)
  }

  def getChannel() = connect().createChannel(3)

  def sendMsgs(msgs: ArrayBuffer[String], exchangeName: String, mqRouteKey: String): Unit = {
    val mqChannel = getChannel
    msgs.foreach { msg =>
      // 声明exchange
      mqChannel.exchangeDeclare(exchangeName, "topic", true)
      // 发送消息, 指定RoutingKey
      mqChannel.basicPublish(
        exchangeName,
        mqRouteKey,
        null,
        msg.toString.getBytes("UTF-8")
      )
      println("____ msg :" + msg)
    }
    //    if (null != mqChannel) mqChannel.close()
  }

  def close() = {
    ConnUtil.close(this)
  }

}
